#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time    : 2024/9/29 15:59 
# @Author  : SumKids 
# @Site    :  
# @File    : 05_window_show.py
# @Software: PyCharm
# @Description:

from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QVBoxLayout
import sys


class MyWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.setWindowTitle('主窗口')
        # self.subWindow = None
        self.subWindow = SubWidget()

        btn = QPushButton('开启/隐藏窗口')
        btn.clicked.connect(self.toggle_sub_window)

        # 创建标签显示子窗口状态
        self.statusLabel = QLabel()

        layout = QVBoxLayout()
        layout.addWidget(btn)
        layout.addWidget(self.statusLabel)
        self.setLayout(layout)

    def toggle_sub_window(self):
        if not self.subWindow:
            self.subWindow = SubWidget()

        # if self.subWindow.isHidden():     # 不推荐这么使用：https://blog.csdn.net/wangw8507/article/details/117475088
        if not self.subWindow.isVisible():
            self.subWindow.show()
        else:
            # self.subWindow.hide()   # 隐藏：占用的内存不会释放，但是页面展示的内容不需要重新加载
            self.subWindow.close()  # 关闭：会释放占用的内存等资源，但是重新打开需要重新加载页面内容

        self.statusLabel.setText("子窗口状态：" + ("显示" if self.subWindow.isVisible() else "隐藏/关闭"))


class SubWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('子窗口')
        label = QLabel('标签：')
        lineEdit = QLineEdit()

        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(lineEdit)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWidget()
    window.show()
    app.exec()
